package com.itboye.mengwu.app.ui.main;

import java.util.Observable;
import java.util.Observer;

import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.GoodsCommentAdapter;
import com.itboye.mengwu.data.model.ShopCarXiangQing;
import com.itboye.mengwu.data.model.ShopPingJiabean;
import com.itboye.mengwu.data.presenter.ShopCarNextPresenter;
import com.itboye.mengwu.goodsdetail.snapscrollview.McoySnapPageLayout.McoySnapPage;
import com.itboye.mengwu.widget.InnerListview;

public class ItemFragment extends BaseFragment implements Observer,McoySnapPage{

	GoodsCommentAdapter adapter;
	private InnerListview goodsconmment_listview;
	private String pid;
	ShopCarNextPresenter presenter;
	ShopCarXiangQing carNext;
	/** ListView对象 */
	ShopPingJiabean jiabean;
	private int page_no = 1;
	private int page_size = 10;
	int num;
	private boolean isAtTop;
	private boolean isIdle;

	public ItemFragment(String pid) {
		this.pid = pid;
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_goodscomment_listview;
	}

	@Override
	public void onMyActivityCreated() {
		presenter = new ShopCarNextPresenter(this);

		carNext = new ShopCarXiangQing();
		carNext.setPid(pid);
		System.out.println("wodePid" + pid);
		presenter.getShopCarXiangQing(pid);
		goodsconmment_listview.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				isIdle = scrollState == OnScrollListener.SCROLL_STATE_IDLE;
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				if (firstVisibleItem == 0 && isIdle) {
					Log.e("hu log", "滑到顶部");
					isAtTop = true;
				}
				if (visibleItemCount + firstVisibleItem == totalItemCount) {
					Log.e("hu log", "滑到底部");
				}
			}
		});

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ShopCarNextPresenter.ShopCarNext_success) {
				carNext = (ShopCarXiangQing) entity.getData();
				adapter = new GoodsCommentAdapter(getActivity(), carNext);
				goodsconmment_listview.setAdapter(adapter);
				System.out.println(">>>>>>>>><<<<<<<<<<<<<" + entity.getData());

				// ByAlert.alert("当前没有数据可加载了...");

			}
		}
	}

	@Override
	public View getRootView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isAtTop() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isAtBottom() {
		// TODO Auto-generated method stub
		return isAtTop;
	}
}
